package com.ys.everydaynews.view

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import android.widget.TextView
import com.ys.everydaynews.R
import com.ys.everydaynews.utils.CommonUtils

class EditTextDialog(title: String) : BaseDialogFragment(), TextWatcher {
    private var btnOk: TextView? = null
    private var btnCancel: TextView? = null
    private var tvNum: TextView? = null
    private var num: Int = 0
    private val title = title

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun initView(view: View) {
        btnOk = view.findViewById(R.id.btn_ok)
        btnCancel = view.findViewById(R.id.btn_cancel)
        tvNum=view.findViewById(R.id.tv_num)
        val tvTitle: TextView = view.findViewById(R.id.tv_title)
        val editName: EditText = view.findViewById(R.id.edit_name)
        tvTitle.text = title
        editName.addTextChangedListener(this)
        val lp: WindowManager.LayoutParams = dialog!!.window!!.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT
        dialog!!.window!!.setGravity(Gravity.BOTTOM)
        val rootView: View = dialog!!.window!!.decorView
       // rootView.viewTreeObserver.addOnGlobalFocusChangeListener(CommonUtils.getGlobalListener())
    }

    override fun initData() {

    }

    override fun getLayout(): Int = R.layout.dialog_edit
    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
    }

    override fun afterTextChanged(p0: Editable?) {
        val content = p0.toString()
        num = 30 - content.length
        tvNum!!.text="$num"
        if (!TextUtils.isEmpty(content)) {
            btnOk!!.setBackgroundResource(R.color.jx_red)
        } else {
            btnOk!!.setBackgroundResource(R.color.jx_gray)
        }
    }
}